package com.vincent.jvmtest.Jdbc;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author vincent
 * @DATE 2018/11/25
 * @Description
 */
public enum JdbcTemplateManager {
    instance;
    private Map<String, DruidDataSource> dataSourceMap = new ConcurrentHashMap<>();
    private Map<String, JdbcTemplate> jdbcTemplateMap = new ConcurrentHashMap<>();

    public void addDataSource(String name, DruidDataSource dataSource) {
        dataSourceMap.put(name, dataSource);
    }

    public DruidDataSource getDataSource(String name) {
        return dataSourceMap.get(name);
    }

    public void removeDataSource(String name) {
        dataSourceMap.remove(name);
    }

    public void changeMaxActive(String name, int maxActive) {
        DruidDataSource druidDataSource = dataSourceMap.get(name);
        int minIdle = druidDataSource.getMinIdle();
        int active = maxActive > minIdle ? maxActive : minIdle;
        druidDataSource.setMaxActive(active);
    }

    public DruidDataSource builtDataSource(Map<String, String> config) {
        Assert.notNull(config, "dataSource config cannot be null");
        DruidDataSource druidDataSource = new DruidDataSource();

        String url = config.get("url");
        Assert.notNull(url, "dataSource url cannot be null");
        druidDataSource.setUrl(url);

        String username = config.get("username");
        Assert.notNull(username, "dataSource username cannot be null");
        druidDataSource.setUsername(username);

        String password = config.get("password");
        Assert.notNull(password, "dataSource password cannot be null");
        druidDataSource.setPassword(password);

        String maxActive = config.get(username);

        int active = StringUtils.isEmpty(maxActive) ? 5 : Integer.parseInt(maxActive);
        // 最大连接处
        druidDataSource.setMaxActive(active);
        // 初始化连接数
        druidDataSource.setInitialSize(5);
        // 最小连接数
        druidDataSource.setMinIdle(5);
        // 连接最长等待时间
        druidDataSource.setMaxWait(60000L);
        // 申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis，执行validationQuery检测连接是否有效。
        druidDataSource.setTestWhileIdle(true);
        // 连接保持空闲而不被驱逐的最小时间
        druidDataSource.setMinEvictableIdleTimeMillis(60 * 60 * 1000);
        return druidDataSource;
    }

    public void addJdbcTemplate(String name, JdbcTemplate jdbcTemplate) {
        jdbcTemplateMap.put(name, jdbcTemplate);
    }

    public void initJdbcTemplate(String name, Map<String, String> config) {
        DruidDataSource druidDataSource = builtDataSource(config);
        addDataSource(name, druidDataSource);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(druidDataSource, true);
        addJdbcTemplate(name, jdbcTemplate);
    }

    public JdbcTemplate getJdbcTemplate(String name) {
        JdbcTemplate jdbcTemplate = jdbcTemplateMap.get(name);
        Assert.notNull(jdbcTemplate, "could not find jdbcTemplate by dataSource name = "+name);
        return jdbcTemplate;
    }

    public void evictJdbcTemplate(String name){
        removeDataSource(name);
        jdbcTemplateMap.remove(name);
    }


}
